import asyncio

import pymysql

db = pymysql.connect(host='dc.unifound.net',
                     port=63307,
                     user='root',
                     password='zjdx20210126',
                     database='uni_center',
                     charset='utf8mb4',  # mysql字符格式
                     cursorclass=pymysql.cursors.DictCursor)
results = []
async def main():
 try:
    cursor = db.cursor()

    sql = """select id,uni_id,book_name from uni_center.c_book LIMIT 10"""

    cursor.execute(sql)

    results = cursor.fetchall()
    inum = 0
    print("id,uni_id,book_name")
    for row in results:
        stu_No = row['id']
        stu_Name = row['uni_id']
        stu_Age = row['book_name']
        # 打印结果
        # print("学号：%s,姓名：%s,年龄：%s,专业：%s"\
        inum += 1
        print("%s,%s,%s" \
              % (stu_No, stu_Name, stu_Age))

    print("\n代码执行完毕...查询到%d条符合条件记录。" % (inum))

 except pymysql.Error as err:
    print(err)

 finally:
    cursor.close()
    db.close()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())